package logger

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

type LogLevel zapcore.Level

// 日志等级
const (
	DebugLevel = LogLevel(zap.DebugLevel)
	InfoLevel  = LogLevel(zap.InfoLevel)
	WarnLevel  = LogLevel(zap.WarnLevel)
	ErrorLevel = LogLevel(zap.ErrorLevel)
	PanicLevel = LogLevel(zap.DPanicLevel)
)

var logLevelString = map[LogLevel]string{
	DebugLevel: "debug",
	InfoLevel:  "info",
	WarnLevel:  "warn",
	ErrorLevel: "error",
	PanicLevel: "panic",
}

// 日志输出目标
const (
	outputBash = iota //输出到终端
	outputFile        //输出到文件
)

type Options func(*logger)

// LogsOutputToFile 日志输出到文件
func LogsOutputToFile(dir string) Options {
	return func(l *logger) {
		l.Target = outputFile
		l.Dir = dir
	}
}

// LogsOutputToBash 日志输出到终端
func LogsOutputToBash() Options {
	return func(l *logger) {
		l.Target = outputBash
	}
}

// LogsFileSplitWithLevel 将日志输出到文件时，对文件进行等级划分
func LogsFileSplitWithLevel() Options {
	return func(l *logger) {
		l.FileLvSplit = true
	}
}

// LogsFileSplitWithDay 将日志输出到文件时，对文件进行天划分
func LogsFileSplitWithDay() Options {
	return func(l *logger) {
		l.FileDaySplit = true
	}
}

// LogsCustomSkipLevel 自定义日志上跳级别
func LogsCustomSkipLevel(skip int) Options {
	return func(l *logger) {
		l.Option = append(l.Option, zap.AddCallerSkip(skip))
	}
}

// LogsCustomTimeFormat 自定义日志时间格式
// 若要使用	[LogsOutputJson] 必须在其之前
func LogsCustomTimeFormat(layout string) Options {
	return func(l *logger) {
		l.Config.EncodeTime = zapcore.TimeEncoderOfLayout(layout)
		l.Encoder = zapcore.NewConsoleEncoder(l.Config)
	}
}

// LogsContextKeys 日志链式context调用keys
func LogsContextKeys(keys ...any) Options {
	return func(l *logger) {
		l.Keys = keys
	}
}

// LogsOutputStack 日志堆栈模式输出
func LogsOutputStack() Options {
	return func(l *logger) {
		l.Option = append(l.Option, zap.AddStacktrace(zapcore.Level(l.Level)))
	}
}

// 日志输出为json格式
func LogsOutputJson() Options {
	return func(l *logger) {
		l.Config.EncodeLevel = zapcore.LowercaseLevelEncoder
		l.Encoder = zapcore.NewJSONEncoder(l.Config)
		l.Interval = ","
	}
}
